package com.ruoyi.petSys.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.petSys.entity.dto.PromAdoptionDto;
import com.ruoyi.petSys.utils.FileUtil;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.petSys.domain.PromAdoption;
import com.ruoyi.petSys.service.IPromAdoptionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 发布领养Controller
 * 
 * @author ruoyi
 * @date 2024-03-22
 */
@RestController
@RequestMapping("/petSys/promAdoption")
public class PromAdoptionController extends BaseController
{
    @Autowired
    private IPromAdoptionService promAdoptionService;

    /**
     * 查询发布领养列表
     */
    @PreAuthorize("@ss.hasPermi('petSys:promAdoption:list')")
    @GetMapping("/list")
    public TableDataInfo list(PromAdoption promAdoption)
    {
        startPage();
        List<PromAdoptionDto> list = promAdoptionService.selectPromAdoptionList(promAdoption);
        return getDataTable(list);
    }

    /**
     * 导出发布领养列表
     */
    @PreAuthorize("@ss.hasPermi('petSys:promAdoption:export')")
    @Log(title = "发布领养", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PromAdoption promAdoption)
    {
        List<PromAdoptionDto> list = promAdoptionService.selectPromAdoptionList(promAdoption);
        ExcelUtil<PromAdoptionDto> util = new ExcelUtil<PromAdoptionDto>(PromAdoptionDto.class);
        util.exportExcel(response, list, "发布领养数据");
    }

    /**
     * 获取发布领养详细信息
     */
    @PreAuthorize("@ss.hasPermi('petSys:promAdoption:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(promAdoptionService.selectPromAdoptionById(id));
    }

    /**
     * 新增发布领养
     */
    @PreAuthorize("@ss.hasPermi('petSys:promAdoption:add')")
    @Log(title = "发布领养", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PromAdoption promAdoption)
    {
        return toAjax(promAdoptionService.insertPromAdoption(promAdoption));
    }

    /**
     * 修改发布领养
     */
    @PreAuthorize("@ss.hasPermi('petSys:promAdoption:edit')")
    @Log(title = "发布领养", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PromAdoption promAdoption)
    {
        return toAjax(promAdoptionService.updatePromAdoption(promAdoption));
    }

    /**
     * 删除发布领养
     */
    @PreAuthorize("@ss.hasPermi('petSys:promAdoption:remove')")
    @Log(title = "发布领养", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(promAdoptionService.deletePromAdoptionByIds(ids));
    }


    @RequestMapping("upload")
    @ResponseBody
    @Deprecated
    public AjaxResult upload(MultipartFile file, HttpServletRequest request){
        AjaxResult responseDto = null;
        String originName = file.getOriginalFilename();
        if(!originName.endsWith(".jpg")){
            return toAjax(false);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd");
        //设置文件夹分类
        String format = sdf.format(new Date());
        String realPath = "/usr/server/springboot/upload"+ format;

        File realFolder = new File(realPath);

        if(!realFolder.exists()){
            realFolder.mkdirs();
        }
        //创建唯一标识以防重名
        String fileName = UUID.randomUUID().toString()+".jpg";
        try {
            file.transferTo(new File(realFolder,fileName));
            String url = "http://127.0.0.1:8080/view"+format+"/"+fileName;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return responseDto;
    }

    @PostMapping("/adoptionUpload")
    @ResponseBody
    public AjaxResult adoptionUpload(MultipartFile file) throws IOException {

        Long userId = SecurityUtils.getUserId();
        String contentType = file.getContentType();
        String filePath = null;
        if(contentType.contains("image")){

        filePath = FileUtil.portalUploadFileByUserId(userId, file, "adoption");
        }
        if(filePath != null){
            return AjaxResult.success("上传成功", filePath);
        }
        return AjaxResult.error();
    }


    @PostMapping("/deleteAdopImg")
    @ResponseBody
    public AjaxResult petUpload(@RequestParam("fileUrl") String fileUrl) throws IOException {

        boolean res = FileUtil.deleteProjectFiles(fileUrl);
        if(res == true){
            return AjaxResult.success("删除成功", res);
        }
        return AjaxResult.error();
    }
}
